package com.weds.edjk.service;

import com.weds.core.base.BaseService;
import com.weds.edjk.dto.MqttCallbackCardDto;
import com.weds.edjk.dto.MqttCallbackUserDto;
import com.weds.edjk.entity.CheckJlEntity;
import com.weds.edjk.mapper.UserMonitorMapper;
import com.weds.edjk.util.EntityUtils;
import com.weds.edjk.vo.MjjlVo;
import com.weds.edjk.vo.PersonalInfoVo;
import com.weds.edjk.vo.ReportMqttVo;
import com.weds.edjk.vo.VisUserVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Author 张从阵
 * @Description 人员基本信息
 * @Date 2020-6-20
 */
@Service
public class UserMonitorService extends BaseService {
    @Resource
    private UserMonitorMapper userMonitorMapper;

    /**
     * 大屏id
     */
    @Value("${mqtt.viewId}")
    private  String viewId;
    /**
     * 页面回调方法
     */
    @Value("${mqtt.cmd}")
    private  String cmd;
    /**
     * 照片主机
     */
    @Value("${weds.scm.root-path}")
    private String rootPath;
    /**
     * 访客档案照片地址
     */
    @Value("${weds.scm.vis-photo-path}")
    private String visPhotoPath;
    /**
     * 人员档案照片
     */
    @Value("${weds.scm.photo-path}")
    private String photoPath;
    /**
     * 即时照片地址
     */
    @Value("${weds.scm.frame-path}")
    private String framePath;
    /**
     * 身份证照片
     */
    @Value("${weds.scm.iden-photo-path}")
    private String idenPhotoPath;
    /**
     * 即时照片前缀
     */
    @Value("${weds.scm.frame-name-prefix}")
    private String frameNamePrefix;
    /**
     * 当前人员监控信息
     * @return
     */
    public ReportMqttVo getCurrentUser(String inStr){
        ReportMqttVo mqttVo = new ReportMqttVo();
        PersonalInfoVo personalInfoVo = new PersonalInfoVo();
        // 人员信息
        String[] split = StringUtils.split(inStr,",");
        // 第一位为用户编号或者身份证号 根据长度区分
        int length = split[0].length();
        // 身份证
        if(18 == length){
            MqttCallbackCardDto mqttCallbackCardDto = new MqttCallbackCardDto();
            mqttCallbackCardDto = EntityUtils.strToEntity(mqttCallbackCardDto, inStr);
            if(Objects.nonNull(mqttCallbackCardDto)){
                personalInfoVo = card(mqttCallbackCardDto);
            }
        }else {
            // 员工卡、访客卡
            MqttCallbackUserDto mqttCallbackUserDto = new MqttCallbackUserDto();
            mqttCallbackUserDto = EntityUtils.strToEntity(mqttCallbackUserDto, inStr);
            if(Objects.nonNull(mqttCallbackUserDto)){
                personalInfoVo = user(mqttCallbackUserDto);
            }
        }
        mqttVo.setViewId(viewId);
        mqttVo.setCmd(cmd);
        mqttVo.setData(personalInfoVo);
        return mqttVo;
    }

    /**
     * 刷卡（内部员工、访客）
     * @param mqttCallbackUserDto
     * @return
     */
    public PersonalInfoVo user(MqttCallbackUserDto mqttCallbackUserDto){
        // 人员信息
        PersonalInfoVo personalInfoVo;
        // 最近访问通道记录
        ArrayList<MjjlVo> recordVos = new ArrayList<>();
        // 区分访客还是人员 识别类型1访客
        if("1".equals(mqttCallbackUserDto.getIdentifyType())){
            // 查询访客表
            personalInfoVo = userMonitorMapper.selectVisUserInfoByVisSerial(mqttCallbackUserDto.getSerial());
            if(Objects.isNull(personalInfoVo)){
                personalInfoVo = new PersonalInfoVo();
            }
            // 1开门成功 0开门失败
            if("100000000".equals(mqttCallbackUserDto.getSerial())){
                personalInfoVo.setIdentifyState("访问失败");
            }else {
                personalInfoVo.setIdentifyState("欢迎来访");
            }
            // 访客档案照片
            String photo = userMonitorMapper.selectVisPhotoByVisSerial(mqttCallbackUserDto.getSerial());
            if(StringUtils.isNotBlank(photo)){
                String substring = photo.substring(photo.lastIndexOf("/photo") + 6);
                personalInfoVo.setPhoto(rootPath+"/"+photoPath+"/"+substring);
            }
            personalInfoVo.setSj(mqttCallbackUserDto.getSj());
            // 查询指定访客的门禁记录
            List<MjjlVo> mjjlVos = userMonitorMapper.selectMjJlByUserSerialAndUserLx(mqttCallbackUserDto.getSerial(), "1");
            personalInfoVo.setMjjls(mjjlVos);
            personalInfoVo.setPersonaType("外来访客");
        }else {
            // 内部人员
            personalInfoVo = userMonitorMapper.selectUserInfoByUserSerial(mqttCallbackUserDto.getSerial());
            if(Objects.isNull(personalInfoVo)){
                personalInfoVo = new PersonalInfoVo();
            }
            if("100000000".equals(mqttCallbackUserDto.getSerial())){
                personalInfoVo.setIdentifyState("识别失败");
            }else {
                personalInfoVo.setIdentifyState("识别成功");
            }
            // 内部人员档案照片
            String photo = userMonitorMapper.selectUserPhotoByUserSerial(mqttCallbackUserDto.getSerial());
            if(StringUtils.isNotBlank(photo)){
                String substring = photo.substring(photo.lastIndexOf("/photo") + 6);
                personalInfoVo.setPhoto(rootPath+"/"+photoPath+"/"+substring);
            }
            personalInfoVo.setSj(mqttCallbackUserDto.getSj());
            // 查询指定人员的门禁记录
            List<MjjlVo> mjjlVos = userMonitorMapper.selectMjJlByUserSerialAndUserLx(mqttCallbackUserDto.getSerial(), "0");
            personalInfoVo.setMjjls(mjjlVos);
            // 内部人员bz为部门
            personalInfoVo.setPersonaType(personalInfoVo.getBz());
        }
        // 记录日期
        String jlDate = StringUtils.substring(mqttCallbackUserDto.getSj(), 0, 10);
        // 临时照片
        String framePhoto = userMonitorMapper.selectjJframePhotoByUserSerial("0", StringUtils.substring(mqttCallbackUserDto.getSj(), 0, 10));
        if(StringUtils.isNotBlank(framePhoto)){
            personalInfoVo.setFramePhoto(rootPath+"/"+framePath+"/"+mqttCallbackUserDto.getDevSerial()+"/"+jlDate+"/"+frameNamePrefix+framePhoto+".jpg");
        }
        // 权限警告 100000000代表失败的记录 或者 活体攻击检测
        if("100000000".equals(mqttCallbackUserDto.getSerial())){
            // 预警
            personalInfoVo.setIsWarn("1");
            personalInfoVo.setWarnContent("暂无权限");
            // 不显示门禁
            personalInfoVo.setMjjls(null);
        }
        // 高温警告 体温状态值:空字符串=无上报信息 N=正常 L=低烧 H=高烧
        if("L".equals(mqttCallbackUserDto.getTemperatureState()) && "H".equals(mqttCallbackUserDto.getTemperatureState())){
            // 预警
            personalInfoVo.setIsWarn("1");
            personalInfoVo.setWarnContent("体温异常 "+mqttCallbackUserDto.getTemperature());
        }
        personalInfoVo.setDevSerial(mqttCallbackUserDto.getDevSerial());
        MjjlVo doorRecordVo = new MjjlVo();
        recordVos.add(doorRecordVo);
        return personalInfoVo;
    }

    /**
     * 刷身份证
     * @param mqttCallbackCardDto
     * @return
     */
    public PersonalInfoVo card(MqttCallbackCardDto mqttCallbackCardDto){
        // 人员信息
        PersonalInfoVo personalInfoVo = new PersonalInfoVo();
        personalInfoVo.setPersonalName(mqttCallbackCardDto.getUserName());
        personalInfoVo.setSerial(mqttCallbackCardDto.getUserCard());
        personalInfoVo.setSj(mqttCallbackCardDto.getSj());
        personalInfoVo.setSex(mqttCallbackCardDto.getUserSex());
        personalInfoVo.setTemperature(mqttCallbackCardDto.getTemperature());
        personalInfoVo.setBz(mqttCallbackCardDto.getAddress());
        personalInfoVo.setPersonaType("未知");
        // 记录状态 1核验通过 2 核验失败 3无效证件 4证件失效
        String identifyState = "";
        switch (mqttCallbackCardDto.getIdentifyState()){
            case "1" : identifyState = "核验通过"; break;
            case "2" : identifyState = "核验失败"; break;
            case "3" : identifyState = "无效证件"; break;
            case "4" : identifyState = "证件失效"; break;
        }
        personalInfoVo.setIdentifyState(identifyState);
        // 高温警告 体温状态值:空字符串=无上报信息 N=正常 L=低烧 H=高烧
        if("L".equals(mqttCallbackCardDto.getTemperatureState()) && "H".equals(mqttCallbackCardDto.getTemperatureState())){
            personalInfoVo.setIsWarn("1");
            personalInfoVo.setWarnContent("体温异常 "+mqttCallbackCardDto.getTemperature());
        }
        // 临时照片
        CheckJlEntity checkJl = userMonitorMapper.selectCardPhotoByUserSerial(mqttCallbackCardDto.getUserCard(), StringUtils.substring(mqttCallbackCardDto.getSj(), 0, 10));
        if(Objects.nonNull(checkJl)){
            personalInfoVo.setPhoto(rootPath+"/"+idenPhotoPath+"/"+mqttCallbackCardDto.getUserCard()+".jpg");
            // 记录日期
            String jlDate = StringUtils.substring(mqttCallbackCardDto.getSj(), 0, 10);
            personalInfoVo.setFramePhoto(rootPath+"/"+framePath+"/"+checkJl.getDevSerial()+"/"+jlDate+"/"+frameNamePrefix+checkJl.getUserFrameSerial()+".jpg");
            personalInfoVo.setDevSerial(checkJl.getDevSerial());
        }
        // 查询指定身份证的门禁记录
        List<MjjlVo> mjjlVos = userMonitorMapper.selectCheckJlByUserId(mqttCallbackCardDto.getUserCard());
        personalInfoVo.setMjjls(mjjlVos);
        return personalInfoVo;
    }

    /**
     * 查询访客详情
     * @param visSerial
     * @return
     */
    public VisUserVo selectVisUserDetailByVisSerial(String visSerial){
        return userMonitorMapper.selectVisUserDetailByVisSerial(visSerial);
    }
}
